﻿#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "ActionEventHandler.h"
#include "CentralWidget.h"
#include "MessageTableWidget.h"
#include "SARibbonBarHelper.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include <QAction>
#include <QPushButton>
#include <QList>
#include <QSplitter>
#include <QProgressBar>
#include <QKeyEvent>
#include <QLabel>


namespace GUI
{
    MainWindow::MainWindow(QWidget *parent) : SARibbonMainWindow(parent)
    { 
        //创建UI对象
        _ui = new Ui::MainWindow;
        _ui->setupUi(this);
        //初始化Ribbon        
        _ribbonBarHelper = new SARibbonBarHelper(this);
        _ribbonBarHelper->initRibbonBar();

        //连接信号槽
        m_ActionHandler = new ActionEventHandler();
        QList<QAction*> actionList = this->findChildren<QAction*>();
        for (QAction* action : actionList) 
        {
            if (action == nullptr)continue;
            connect(action, SIGNAL(triggered()), this->getActionEventHandle(), SLOT(execOperator()));
        }
 
        //初始化窗口
        this->initWindow();
        this->setWindowTitle("Marine Noise");
    }

    MainWindow::~MainWindow()
    {
        if (m_ActionHandler) delete m_ActionHandler;
        if (_ribbonBarHelper) delete _ribbonBarHelper;
        if (_ui) delete _ui;
    }

    ActionEventHandler * MainWindow::getActionEventHandle() const
    {
        return m_ActionHandler;
    }

    MainWindow* MainWindow::GetMainWindowFromFramework()
    {
        //访问全局数据
        AppFrame::FITKGlobalData* data = FITKAPP->getGlobalData();
        if (data == nullptr) return nullptr;
        QWidget* w = data->getMainWindow();
        //强制类型转换
        return dynamic_cast<MainWindow*>(w);
    }

    GUI::CentralWidget* MainWindow::getCentralWidget()
    {
        return _centralWidget;
    }

    void MainWindow::setProgressValue(int value)
    {
        if (_progerssBar == nullptr) return;
        //设置进度值
        _progerssBar->setValue(value);
        //设置可见性 只有在(0,100) 区间可见
        _progerssBar->setVisible(value < 100 && value > 0);
        if(_processLabel)
            _processLabel->setVisible(value < 100 && value > 0);
    }

    void MainWindow::setProcessLabel(const QString& text)
    {
        if (_processLabel == nullptr) return;
        _processLabel->setText(text);
    }

    void MainWindow::keyPressEvent(QKeyEvent * event)
    {
        if (event && _ribbonBarHelper) {
            int key = event->key();
            if (key == Qt::Key_F2 || key == Qt::Key_F5) {
                //如果模块不在当前需要的模块，先切换模块，再模拟信号发出
                if (_ribbonBarHelper->getCurrentModuleType() != SARibbonBarHelper::GUIModuleType::Tool) {
                    _ribbonBarHelper->switchToModule(SARibbonBarHelper::GUIModuleType::Tool);
                    QKeyEvent *pressEvent = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier);
                    QCoreApplication::sendEvent(this, pressEvent);
                }
            }
        }
        SARibbonMainWindow::keyPressEvent(event);
    }

    void MainWindow::initWindow()
    {
        _centralWidget = new CentralWidget(this);
        _messageTab = new MessageTabWidget(this);

        // 子部件水平排布
        QSplitter *spliterLayout = new QSplitter(Qt::Vertical,this);
        spliterLayout->setMouseTracking(true);
        spliterLayout->setHandleWidth(5);

        //添加树界面
        spliterLayout->addWidget(_centralWidget);
        //添加属性界面
        spliterLayout->addWidget(_messageTab);
        // 设置大小
        spliterLayout->setSizes({ 20000, 6000});        
        this-> setCentralWidget(spliterLayout);
        //进度条
        _progerssBar = new QProgressBar(this);
        _processLabel = new QLabel(this);
        _progerssBar->setRange(0, 100);
        this->statusBar()->addPermanentWidget(_processLabel);
        this->statusBar()->addPermanentWidget(_progerssBar);
        this->setProgressValue(0);
    }

  

}
